<?php
class B2BAlipay{
	public $notify_url  = '';//异步处理地址
	public $return_url  = '';//同步处理地址
	public $show_url    = '';//商品展示地址
	public $partner     = '2088511514309883';//合作身份者id
	public $key         = 'h4jp5pkrwex869mzozzi6oio3i0u5z84';//安全检验码
	public $sign_type   = 'MD5';//签名方式
	public $cacert_path = '';//签名方式
	public $transport   = 'http';//访问模式
	public $is_debug    = false;//true为调试模式，价格会设置成0.01，false为非调试模式，价格按正常来
	public $logs_path   = '';

	public function __construct() {
        //导入支付宝alipay_submit包
		import("alipay_submit",'Public/Class/Pay/Alipay/lib/B2B');
        //导入支付宝alipay_notify包
		import("alipay_notify",'Public/Class/Pay/Alipay/lib/B2B');
		//日志保存目录
		$this->logs_path = dirname(__FILE__).'/Logs/';
		if(file_exists($this->logs_path)==false){
			mkdir($this->logs_path,true);
		}
    }
	/**
	 * 平台商支付宝配置
	 */
	public function config(){
		//合作身份者id，以2088开头的16位纯数字
		$alipay_config['partner']       = $this->partner;
		//安全检验码，以数字和字母组成的32位字符
		$alipay_config['key']           = $this->key;
		//签名方式 不需修改
		$alipay_config['sign_type']     = strtoupper($this->sign_type);
		//字符编码格式 目前支持 gbk 或 utf-8
		$alipay_config['input_charset'] = strtolower('utf-8');
		//ca证书路径地址，用于curl中ssl校验
		//请保证cacert.pem文件在当前文件夹目录中
		$cacert_path                    = dirname(__FILE__).'/alipay_cacert.pem';
		$alipay_config['cacert']        = $this->cacert_path?$this->cacert_path:$cacert_path;
		//访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
		$alipay_config['transport']     = $this->transport;

		return $alipay_config;
	}
	public function submit($seller_email,$order_sn,$order_title,$payed,$order_desc){
		//调试模式价格设置0.01元
		if($this->is_debug==true){
			$payed = 0.01;
		}
		$alipay_config = $this->config();
		/**************************请求参数**************************/
	    //支付类型
		$payment_type      = "1";
		//必填，不能修改
		$host              =  'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'];
		//异步请求URL
		$notify_url        = $this->notify_url?$this->notify_url:$host.U('Alipay/notify');
		//页面跳转同步通知页面路径
		$return_url        = $this->return_url?$this->return_url:$host.U('Alipay/call_back');
		//商品展示地址
		$show_url          = $this->show_url?$this->show_url:$host.U('Order/detail',array('order_sn'=>$order_sn));
		
		//防钓鱼时间戳
		$anti_phishing_key = "";
		//若要使用请调用类文件submit中的query_timestamp函数
		//客户端的IP地址
		$exter_invoke_ip   = "";
	    //非局域网的外网IP地址，如：221.0.0.1
		/************************************************************/

		//构造要请求的参数数组，无需改动
		$parameter = array(
				"service"           => "create_direct_pay_by_user",
				"partner"           => trim($alipay_config['partner']),
				"payment_type"      => $payment_type,
				"notify_url"        => $notify_url,
				"return_url"        => $return_url,
				"seller_email"      => $seller_email,//卖家支付宝帐户
				"out_trade_no"      => $order_sn,
				"subject"           => $order_title,
				"total_fee"         => $payed,
				"body"              => $order_desc,
				"show_url"          => $show_url,
				"anti_phishing_key" => $anti_phishing_key,
				"exter_invoke_ip"   => $exter_invoke_ip,
				"_input_charset"    => trim(strtolower($alipay_config['input_charset']))
		);
		//建立请求
		$alipaySubmit = new AlipaySubmit($alipay_config);
		$alipaySubmit->jump_pay($parameter);
	}
}
?>